<template>
    <div class="app-container">
        <header-title/>
        <el-form
            :model="queryParams"
            ref="queryForm"
            :inline="true"
            v-show="showSearch"
            label-width="110px"
        >
            <el-form-item label="关键词" prop="keyword">
                <el-input
                    v-model="queryParams.keyword"
                    placeholder="请输入订单号/手机号/订单标题"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="用户" prop="userId">
                <el-select v-model="queryParams.userId" filterable clearable>
                    <el-option
                        v-for="dict in userList"
                        :key="dict.clientUserId"
                        :label="dict.userRealName"
                        :value="dict.clientUserId"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
            <!-- <el-form-item label="订单状态" prop="orderStatus">
                <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable>
                    <el-option
                        v-for="dict in dict.type.order_status"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    >

                    </el-option>
                </el-select>
            </el-form-item> -->
           
           
            <el-form-item label="调用ai服务类型" prop="useAiType">
                <el-select v-model="queryParams.useAiType" clearable>
                    <el-option
                        v-for="dict in dict.type.ai_type"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    >
                    </el-option>
                </el-select>

            </el-form-item>
            <el-form-item label="订单类型" prop="orderType">
                <el-select v-model="queryParams.orderType" placeholder="请选择订单类型" clearable>
                    <el-option
                        v-for="dict in dict.type.order_type"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    >

                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="订单发起时间" prop="jiTime">
                <el-date-picker
                    v-model="dateRange"
                    type="daterange"
                    value-format="yyyy-MM-dd"
                    @keyup.enter.native="handleQuery"
                    placeholder="选择订单发起时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="订单结束时间" prop="jiTime">
                <el-date-picker
                    v-model="dateRangeTwo"
                    type="daterange"
                    value-format="yyyy-MM-dd"
                    @keyup.enter.native="handleQuery"
                    placeholder="选择订单结束时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item class="mar-r-20">
                <el-button type="primary" icon="el-icon-search" @click="handleQuery"
                >搜索</el-button
                >
                <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-table
            v-loading="loading"
            :data="tableList"
        >   
        <!-- 订单ID，订单类型，用户手机号，调用服务，消耗算力，订单日期，订单状态 -->
            <el-table-column label="订单ID" >
                <template slot-scope="scope">
                    {{ scope.row.orderId ||'-' }}
                </template>
            </el-table-column>
            <el-table-column label="订单类型" >
                <template slot-scope="scope">
                    {{selectDictLabel(dict.type.order_type,scope.row.orderType ) ||'-'}}
                </template>
            </el-table-column>
            <el-table-column label="用户信息"  min-width="150">
                <template slot-scope="scope">
                    <el-form size="mini" label-suffix="：" class="form-detail-box">
                        <el-form-item label="">
                        <div class="fw-b">{{ scope.row.orderUserName || "-" }}</div>
                            
                        </el-form-item>
                        <el-form-item label="联系方式" class="label-fw-initial">
                            <span>{{ scope.row.phone || "-" }}</span>
                        </el-form-item>
                    </el-form>
                </template>
            </el-table-column>
            <el-table-column label="调用服务" >
                <template slot-scope="scope">
                    {{selectDictLabel(dict.type.ai_type,scope.row.useAiType ) ||'-'}}
                </template>
            </el-table-column>
            <el-table-column label="消耗算力" >
                <template slot-scope="scope">
                    {{ scope.row.changePoint ||'-' }}
                </template>
            </el-table-column>
            <el-table-column label="订单状态" >
                <template slot-scope="scope">
                    <el-tag
                        v-if="scope.row.orderStatus"
                        :type="
                        [3].includes(scope.row.orderStatus)
                            ? 'success'
                            : [0].includes(scope.row.orderStatus)
                            ? 'info'
                            : [2].includes(scope.row.orderStatus)
                            ? 'danger'
                            : [1].includes(scope.row.orderStatus)
                            ? 'warning':''
                        "
                        >
                        {{
                        selectDictLabel(
                            dict.type.order_status,
                            scope.row.orderStatus
                        ) || "未知状态"
                        }}
                        </el-tag
                    >
                    
                </template>
            </el-table-column>
            <el-table-column label="订单日期" >
                <template slot-scope="scope">
                    {{ scope.row.createTime ||'-' }}
                </template>
            </el-table-column>
            <el-table-column label="操作" width="100">
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-search"
                        @click="handleUpdateDetail(scope.row)"
                    >详情</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        >
        </pagination>
        <el-drawer
            title="订单详情"
            :visible.sync="showDetail"
            direction="rtl"
            :before-close="handleClose"
            size="900px"
        >
            <orderDetail :id="detailId" v-if="showDetail"></orderDetail>
        </el-drawer>
    </div>
</template>
<script>
    import {listOrder} from '@/api/computeOrderManage/orderList'
    import {listClientUser} from '@/api/operateManage/userList'
 import orderDetail from '../components/orderDetail/detail.vue'
 
    export default{
        name:'ConsumeList',
        components:{orderDetail},
        dicts:['order_status','order_type','point_change_type','recharge_status','recharge_type','ai_type'],
        data() {
            return {
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    orderType:'',
                    orderStatus:3,
                    createTimeEnd: '',
                    createTimeStart:'',
                    endTimeEnd:'',
                    endTimeStart:'',
                    keyword:'',
                    pointChangeType: 2,
                    rechargeStatus: '',
                    rechargeType: '',
                    useAiType:'',
                    userId: '',
                   
                },
                showSearch: true,
                total: 0,
                dateRange: [],
                dateRangeTwo:[],
                // 表格数据
                tableList: [],
                loading: false,
                userList: [],
                showDetail: false,
                detailId:''
            }
        },
        created() {
            this.getList()
            this.getUserList()
        },
        methods: {
            handleClose() {
                this.showDetail = false
            },
            getUserList() {
                listClientUser({pageSize: 10000,pageNum:1}).then(res=> {
                    this.userList = res.data.records
                })
            },
            handleUpdateDetail(row) {
                this.detailId = row.orderNo
                this.showDetail = true
            },
             /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.dateRange = [];
                this.dateRangeTwo = []
                this.resetForm("queryForm");
                this.handleQuery();
            },
            getList() {
                this.loading = true;
                if (this.dateRange && this.dateRange.length) {
                    this.queryParams.createTimeStart = this.dateRange[0];
                    this.queryParams.createTimeEnd = this.dateRange[1];
                } else {
                    this.queryParams.createTimeStart = undefined;
                    this.queryParams.createTimeEnd = undefined;
                }
                if (this.dateRangeTwo && this.dateRangeTwo.length) {
                    this.queryParams.endTimeStart = this.dateRangeTwo[0];
                    this.queryParams.endTimeEnd = this.dateRangeTwo[1];
                } else {
                    this.queryParams.endTimeStart = undefined;
                    this.queryParams.endTimeEnd = undefined;
                }
                
                listOrder(this.queryParams).then((response) => {
                    this.tableList = response.data.records;
                    this.total = response.data.total;
                    this.loading = false;
                });
            }
        }
    }
</script>